﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Microsoft.AspNetCore.Mvc;
using Sunny.Model.Vo;
using Sunny.Service;

namespace Sunny.Api.Controllers.Monitor
{
    
    [ApiController]
    public class ServerController
    {
        private readonly RedisService _redisService;

        public ServerController(RedisService redisService)
        {
            _redisService = redisService;
        }
        
        [HttpGet]
        [Route("monitor/server")]
        public Result Info()
        {
            ServerInfoVo serverInfo = new ServerInfoVo();
            var ip = NetworkInterface
                .GetAllNetworkInterfaces()
                .Select(p => p.GetIPProperties())
                .SelectMany(p => p.UnicastAddresses)
                .FirstOrDefault(p => p.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p.Address))?.Address.ToString();
            serverInfo.ComputerName = Environment.MachineName;
            serverInfo.CpuCore = Environment.ProcessorCount.ToString();
            serverInfo.ComputerIp = "";
            serverInfo.Memory = (Environment.WorkingSet/1024/1024).ToString("0.##")+"M";
            serverInfo.OsName = Environment.OSVersion.VersionString;
            serverInfo.ComputerIp = ip;
            var runTimespan = TimeSpan.FromMilliseconds(Environment.TickCount);
            serverInfo.RunTime = $"{runTimespan.Days}天{runTimespan.Hours}小时{runTimespan.Minutes}分钟";
            
            List<DiscInfo> discInfos = new List<DiscInfo>();
            foreach (var logicalDrive in Environment.GetLogicalDrives())
            {
                DriveInfo driveInfo = new DriveInfo(logicalDrive);
                
                DiscInfo discInfo = new DiscInfo();
                discInfo.DirName = logicalDrive;
                discInfo.Free = ((decimal)driveInfo.AvailableFreeSpace/1024/1024/1024).ToString("0.##")+"GB";
                discInfo.Total = ((decimal)driveInfo.TotalSize/1024/1024/1024).ToString("0.##")+"GB";
                discInfo.Used =  ((decimal)(driveInfo.TotalSize-driveInfo.AvailableFreeSpace)/1024/1024/1024).ToString("0.##")+"GB";
                discInfo.Usage =  ((decimal)(driveInfo.TotalSize - driveInfo.AvailableFreeSpace)/driveInfo.TotalSize*100).ToString("0.##");
                discInfo.SysTypeName = driveInfo.DriveFormat;
                discInfo.TypeName = driveInfo.DriveType.ToString();
                discInfos.Add(discInfo);
            }
            serverInfo.Disc = discInfos;
            
            return Result.Success(serverInfo);
        }
        
        [HttpGet]
        [Route("monitor/cache")]
        public Result Cache()
        {
            var info = _redisService.server.Info();
            var r =  info.ToDictionary(p => p.Key, p => p.ToDictionary(x => x.Key, x => x.Value));
            return Result.Success(r);
        }
    }
}